package com.dq.controller.app.statistics;

import com.alibaba.fastjson.JSONObject;
import com.dq.dto.SaleStatisticeDto;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.*;
import com.dq.utils.DateUtils;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.statistics.Statistics;
import com.dq.service.AllService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/29 0029 11:40:23
 */
@RestController
@RequestMapping("/api/merchant/statistics")
@Api(description = "统计")
public class MerchantStatisticsController extends AllService {

    @ApiOperation("消费排行榜")
    @PostMapping("countSaleStatistics")
    public Vo countSale(@RequestBody SaleStatisticsQuery saleStatisticsQuery) throws AdminException{

//        Date dayBegin = DateUtils.getDayBegin(new Date(), 0);
//        Date dayEnd = DateUtils.getDayEnd(new Date(), 0);
//        if(saleStatisticsQuery.getBeginTime() == null) {
//            saleStatisticsQuery.setBeginTime(dayBegin.getTime());
//        }
//        if(saleStatisticsQuery.getEndTime() == null) {
//            saleStatisticsQuery.setEndTime(dayEnd.getTime());
//        }

        Gson gson = new Gson();
        String s = gson.toJson(saleStatisticsQuery);
        SaleStatisticsQuery saleStatisticsQuery1 = gson.fromJson(s, SaleStatisticsQuery.class);
        saleStatisticsQuery1.setPage(1);
        saleStatisticsQuery1.setSize(3);
        saleStatisticsQuery1.setOrgId(null);
        // 顾客消费排行
        List<SaleStatisticeDto> saleStatisticeDtos = statisticsService.countSale(saleStatisticsQuery1, false);
        List<SaleStatisticeDto> dtos = statisticsService.countSale(saleStatisticsQuery, true);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rank", saleStatisticeDtos);
        jsonObject.put("list", dtos);


        return new Vo(jsonObject);
    }

    @ApiOperation("业绩排行榜")
    @PostMapping("achievementCount")
    public Vo achievementCount(@RequestBody CountAchievementQuery countAchievementQuery) throws AdminException{

        return new ListDataVo(statisticsService.achievementList(countAchievementQuery), statisticsService.achievementCount(countAchievementQuery));
    }


    @ApiOperation("满意度排行榜")
    @PostMapping("evaluateCount")
    public Vo evaluateCount (@RequestBody EvaluateCountQuery evaluateCountQuery) throws AdminException{
        return new Vo(statisticsService.evaluateCount(evaluateCountQuery));
    }
    @ApiOperation("满意度排行榜根据门店查列表")
    @GetMapping("listByEvaluate/{orgId}")
    public Vo listByEvaluate(@PathVariable Long orgId) throws AdminException{
        return new Vo(statisticsService.listByEvaluate(orgId));
    }

    @ApiOperation("员工预约数")
    @PostMapping("subscribeCount")
    public Vo subscribeCount(@RequestBody SubscribeCountQuery subscribeCountQuery) throws AdminException{
        return statisticsService.subscribeCount(subscribeCountQuery);
    }

    @ApiOperation("test")
    @PostMapping("test")
    public Vo test(){
        System.out.println(statisticsRepository.countSaleByMonth(Statistics.StatisticsType.salePrice));
        return new Vo(statisticsRepository.countSaleByMonth(Statistics.StatisticsType.salePrice));
    }

    @ApiOperation("项目统计")
    @PostMapping("projectCount")
    public Vo projectCount(@RequestBody ProjectStatisticsQuery projectStatisticsQuery) throws AdminException{

        return new Vo(statisticsService.projectCount(projectStatisticsQuery));
    }


}
